//
//  UserViewController.m
//  iengage
//
//  Created by Luis Ziliani on 2/13/09.
//  Copyright 2009 Manas. All rights reserved.
//

#import "CurrentUserViewController.h"
#import "Util.h"
#import "HtmlUserPageConstructor.h"
#import "iengageAppDelegate.h"
#import "SendMessageController.h"

@implementation CurrentUserViewController

@synthesize user, webView, navigationController, updateLocationButton, updateProgress;


- (NSString *) getHTMLForUser {
	HtmlUserPageConstructor *constructor = [[[HtmlUserPageConstructor alloc] init] autorelease];
	[constructor appendHeader];
	[constructor startBody];
	[constructor appendImageWithData: [user imageData]];
	[constructor appendMapImageWithLatitude:[user latitude] 
								  longitude:[user longitude]
									andMark:[[user displayName] substringToIndex:1]];
	[constructor endBody];
	[constructor end];
	return [constructor html];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil user:(User*)userToShow {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		[self setUser: userToShow];
		self.title = [user displayName];
	}
	return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[webView loadHTMLString:[self getHTMLForUser] baseURL:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

- (IBAction)updateLocation:(id)sender {
	@try {
		IEngageAppDelegate * app = [[UIApplication sharedApplication] delegate];
		
		NSMutableString * url = [NSMutableString stringWithString:[app serverURL]];
		[url appendString:@"/setLocation?"];
		[url appendFormat:@"latitude=%.12f&longitude=%.12f", [user latitude], [user longitude]];
		HttpRequestWithLogin *request = [[HttpRequestWithLogin alloc] init];
		[request openURL:url 
				withUser:[app currentUser] 
			 andPassword:[app userPassword] 
				delegate:self];
		
		[request autorelease];
		[updateProgress startAnimating];
		[updateLocationButton setEnabled:NO];
	}
	@catch (NSException * e) {
		[self informError:[e description]];
	}
}


- (IBAction)sendMessage:(id)sender {
	
	SendMessageController* controller = [[SendMessageController alloc] initWithNibName:@"SendMessageView" 
																								bundle:nil];
	
	controller.navigationController = navigationController;
	
	[navigationController pushViewController:controller animated:YES];
	
	[controller autorelease];	
}


- (void)dealloc {
	[webView release];
	[user release];
	[updateProgress release];
	[updateLocationButton release];

    [super dealloc];
}


- (void) informUnkownError {
	[updateProgress stopAnimating];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:   @"Could not update your location."  
												   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
	[alert show];
	[alert release];
}

- (void) informError: (NSString*) error {
	[updateProgress stopAnimating];
	NSString * msg = [[NSString alloc] initWithFormat:@"There has been an error during the location updating. Perhaps a bad configuration? The error is %@", error];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message: msg
												   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
	[alert show];
	[alert release];	
	[msg release];
}

- (void) showPreferencesCredentialsAreIncorrect {
	[updateProgress stopAnimating];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"User or password incorrect" message:   @"The user and password provided are incorrect. Please go to \"Settings -> iengage\" and check them."  
												   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
	[alert show];
	[alert release];
}

- (void) requestEndedOk {
	[updateLocationButton setEnabled:YES];
	[updateProgress stopAnimating];
}

@end
